home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 126-150 / disk_147 / sys / amiga / iconify.zoo / iconify / iconify.c next >
C/C++ Source or Header  |  1986-11-09  |  5KB  |  212 lines

  1. /*  :ts=8 bk=0
  2.  *
  3.  * iconify.c:    You asked for it, you got it.
  4.  *
  5.  * Copyright 1987 by Leo L. Schwab.
  6.  * Permission is hereby granted for use in any and all programs, both
  7.  * Public Domain and commercial in nature, provided this Copyright notice
  8.  * is left intact.  Purveyors of programs, at their option, may wish observe
  9.  * the following conditions (in the spirit of hackerdom):
  10.  *    1: You send me a free, registered copy of the program that uses the
  11.  *       iconify feature,
  12.  *    2: If you're feeling really nice, a mention in the program's
  13.  *       documentation of my name would be neat.
  14.  *
  15.  *                     8712.10        (415) 456-3960
  16.  */
  17. #include <exec/types.h>
  18. #include <devices/timer.h>
  19. #include <intuition/intuition.h>
  20. #include "iconify.h"
  21.  
  22. /*
  23.  * It is recommended that the tick rate not be made too rapid to avoid
  24.  * bogging down the system.
  25.  */
  26. #define    TICKS_PER_SECOND    10
  27.  
  28. /*
  29.  * Some programmers may not wish to have the added functionality of the
  30.  * ICON_FUNCTION feature.  If you're such a programmer, you may comment out
  31.  * the following #define, which will eliminate the code to handle function
  32.  * calls, and make iconify() even smaller.
  33.  */
  34. #define    USE_FUNCTIONS
  35.  
  36. /*
  37.  * Jim Mackraz suggested making icons easily identifiable by outside
  38.  * programs, so this constant gets stuffed into the UserData field.
  39.  */
  40. #define    ICON    0x49434f4eL        /*  'ICON'  */
  41.  
  42.  
  43. extern void    *OpenWindow(), *GetMsg(), *CreatePort(), *CreateExtIO(),
  44.         *CheckIO();
  45. extern long    OpenDevice(), DoubleClick();
  46.  
  47.  
  48. static struct Gadget gadget = {
  49.     NULL,
  50.     0, 0, 0, 0,
  51.     NULL,                /*  Set later  */
  52.     GADGIMMEDIATE,
  53.     WDRAGGING,            /*  Observe the Magic!  */
  54.     NULL,                /*  Set later  */
  55.     NULL, NULL, NULL, NULL,
  56.     0, 0
  57. };
  58.  
  59. static struct NewWindow windef = {
  60.     0, 0, 0, 0,            /*  Set later  */
  61.     -1, -1,
  62.     GADGETDOWN,
  63.     BORDERLESS | SMART_REFRESH | NOCAREREFRESH,
  64.     &gadget,
  65.     NULL, NULL, NULL, NULL,        /*  Lotsa these  */
  66.     0, 0, 0, 0,
  67.     WBENCHSCREEN
  68. };
  69.  
  70. static struct Window        *win;
  71.  
  72. #ifdef USE_FUNCTIONS
  73. static struct timerequest    *tr;
  74. static struct MsgPort        *reply;
  75. #endif
  76.  
  77.  
  78. iconify (left, top, width, height, screen, ptr, type)
  79. UWORD *left, *top, width, height;
  80. struct Screen *screen;
  81. APTR ptr;
  82. int type;
  83. {
  84.     register struct IntuiMessage    *msg;
  85.     long                secs = 0, mics = 0,
  86.                     cs, cm,
  87.                     class,
  88.                     sigmask;
  89.  
  90.     windef.LeftEdge        = *left;
  91.     windef.TopEdge        = *top;
  92.     windef.Width        = width;
  93.     windef.Height        = height;
  94.     windef.Type = (windef.Screen = screen) ? CUSTOMSCREEN : WBENCHSCREEN;
  95.  
  96.     gadget.Flags        = GADGHCOMP | GRELWIDTH | GRELHEIGHT;
  97.  
  98.     switch (type & 3) {
  99.     case ICON_IMAGE:
  100.         gadget.Flags        |= GADGIMAGE;
  101.     case ICON_BORDER:
  102.         gadget.GadgetRender    = ptr;
  103.         break;
  104.  
  105.     case ICON_FUNCTION:
  106. #ifdef USE_FUNCTIONS
  107.         gadget.GadgetRender    = NULL;
  108. #else
  109.         return (0);
  110. #endif
  111.         break;
  112.  
  113.     default:
  114.         return (0);
  115.     }
  116.  
  117.     if (!openstuff ())
  118.         return (0);
  119.     sigmask = 1L << win -> UserPort -> mp_SigBit;
  120.  
  121. #ifdef USE_FUNCTIONS
  122.     if (type == ICON_FUNCTION) {
  123.         sigmask |= 1L << reply -> mp_SigBit;
  124.         tr -> tr_node.io_Command= TR_ADDREQUEST;
  125.         tr -> tr_time.tv_secs    = 0;
  126.         tr -> tr_time.tv_micro    = (1000000L / TICKS_PER_SECOND);
  127.         SendIO (tr);
  128.         /*
  129.          * Make initialization call to user's function.
  130.          * Isn't typecasting wonderful?  :-|
  131.          */
  132.         (* ((void (*)()) ptr)) (win, (WORD) 1);
  133.     }
  134. #endif
  135.  
  136.     while (1) {
  137.         Wait (sigmask);
  138.  
  139. #ifdef USE_FUNCTIONS
  140.         if (GetMsg (reply)) {
  141.             /*
  142.              * Call user's function to do something to the icon.
  143.              */
  144.             (* ((void (*)()) ptr)) (win, (WORD) 0);
  145.             tr -> tr_time.tv_secs    = 0;
  146.             tr -> tr_time.tv_micro    =
  147.              (1000000L / TICKS_PER_SECOND);
  148.             SendIO (tr);
  149.         }
  150. #endif
  151.  
  152.         if (msg = GetMsg (win -> UserPort)) {
  153.             class = msg -> Class;
  154.             cs = msg -> Seconds;
  155.             cm = msg -> Micros;
  156.             ReplyMsg (msg);
  157.  
  158.             if (class == GADGETDOWN) {
  159.                 if (DoubleClick (secs, mics, cs, cm))
  160.                     break;
  161.                 secs = cs;  mics = cm;
  162.             }
  163.         }
  164.     }
  165.  
  166. #ifdef USE_FUNCTIONS
  167.     if (type == ICON_FUNCTION) {
  168.         AbortIO (tr);
  169.         WaitIO (tr);
  170.     }
  171. #endif
  172.  
  173.     *left = win -> LeftEdge;
  174.     *top = win -> TopEdge;
  175.     closestuff ();
  176.     return (1);
  177. }
  178.  
  179. static
  180. openstuff ()
  181. {
  182.     if (!(win = OpenWindow (&windef)))
  183.         return (0);
  184.     win -> UserData = (BYTE *) ICON;
  185.         
  186. #ifdef USE_FUNCTIONS
  187.     if (!(reply = CreatePort (NULL, NULL)) ||
  188.         !(tr = CreateExtIO (reply, (long) sizeof (*tr))) ||
  189.         OpenDevice (TIMERNAME, UNIT_VBLANK, tr, 0L)) {
  190.         closestuff ();
  191.         return (0);
  192.     }
  193. #endif
  194.  
  195.     return (1);
  196. }
  197.  
  198. static
  199. closestuff ()
  200. {
  201. #ifdef USE_FUNCTIONS
  202.     if (tr) {
  203.         if (tr -> tr_node.io_Device)
  204.             CloseDevice (tr);
  205.         DeleteExtIO (tr, (long) sizeof (*tr));
  206.     }
  207.     if (reply)        DeletePort (reply);
  208. #endif
  209.  
  210.     if (win)        CloseWindow (win);
  211. }
  212.